package com.example.springboot2.Service;

import com.example.springboot2.DB_Helper.JDBCUtils;
import com.example.springboot2.Model.TestModel;

import java.sql.*;
import java.util.List;

public class TestService {
    public List<TestModel>  GetList() {
        Connection conn = JDBCUtils.findConnection();      //JDBCUtils 自己定义的一个类
        PreparedStatement pre = null;
        ResultSet res = null;
        String sql = "select * from Device_Base_Fault";
        try {
            pre = conn.prepareStatement(sql);
//            pre.setString(1,"wqq");
            res = pre.executeQuery();
            //调用将结果集转换成实体对象方法
            List<TestModel> list = JDBCUtils.Populate(res,TestModel.class);  //.populate(res, TestModel.class);
            //循环遍历结果
            for(int i=0;i<list.size();i++){
                TestModel user = (TestModel) list.get(i);
//                System.out.println("[username = "+ user.getUsername()+",passwd = "+ user.getPassword()+"]");
            }
            return  list;

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;

    }

}
